﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WebRSSBuilder.Core
{
    public class RSSObjectDataProvider
    {
        #region Private Data Members
        private RSSFeed Feed;
        private IRSSFeedController controller;
        private string url;
        private bool IsChanged = false; 
        #endregion
        #region Properties
        public IRSSFeedController Controller { get { return this.controller; } set { this.controller = value; } }
        public string URL { get { return this.url; } set { this.url = value; } }
        public string Title
        {
            get { return Feed.Title; }
            set { Feed.Title = value; IsChanged = true; }
        }

        public string Generator
        {
            get { return Feed.Generator; }
            set { Feed.Generator = value; IsChanged = true; }
        }

        public string Docs
        {
            get { return Feed.Docs; }
            set { Feed.Docs = value; IsChanged = true; }
        }

        public string CloudDomain
        {
            get { return Feed.CloudDomain; }
            set { Feed.CloudDomain = value; IsChanged = true; }
        }

        public string CloudPort
        {
            get { return Feed.CloudPort; }
            set { Feed.CloudPort = value; IsChanged = true; }
        }

        public string CloudPath
        {
            get { return Feed.CloudPath; }
            set { Feed.CloudPath = value; IsChanged = true; }
        }

        public string CloudRegisterProcedure
        {
            get { return Feed.CloudRegisterProcedure; }
            set { Feed.CloudRegisterProcedure = value; IsChanged = true; }
        }

        public string CloudProtocol
        {
            get { return Feed.CloudProtocol; }
            set { Feed.CloudProtocol = value; IsChanged = true; }
        }

        public string Ttl
        {
            get { return Feed.Ttl; }
            set { Feed.Ttl = value; IsChanged = true; }
        }
        
        public string Rating
        {
            get { return Feed.Rating; }
            set { Feed.Rating = value; IsChanged = true; }
        }

        public string TextInput
        {
            get { return Feed.TextInput; }
            set { Feed.TextInput = value; IsChanged = true; }
        }

        public string SkipDays
        {
            get { return Feed.SkipDays; }
            set { Feed.SkipDays = value; IsChanged = true; }
        }

        public string SkipHours
        {
            get { return Feed.SkipHours; }
            set { Feed.SkipHours = value; IsChanged = true; }
        }

        public string Link
        {
            get { return Feed.Link; }
            set { Feed.Link = value; IsChanged = true; }
        }

        public string Category
        {
            get { return Feed.Category; }
            set { Feed.Category = value; IsChanged = true; }
        }

        public string ManagiongEditor
        {
            get { return Feed.ManagiongEditor; }
            set { Feed.ManagiongEditor = value; IsChanged = true; }
        }

        public string WebURL
        {
            get { return Feed.WebURL; }
            set { Feed.WebURL = value; IsChanged = true; }
        }

        public string Copyright
        {
            get { return Feed.Copyright; }
            set { Feed.Copyright = value; IsChanged = true; }
        }
        public string Language
        {
            get { return Feed.Language; }
            set { Feed.Language = value; IsChanged = true; }
        }
        public string Editor
        {
            get { return Feed.Editor; }
            set { Feed.Editor = value; IsChanged = true; }
        }
        public string Webmaster
        {
            get { return Feed.Webmaster; }
            set { Feed.Webmaster = value; IsChanged = true; }
        }
        public string Description
        {
            get { return Feed.Description; }
            set { Feed.Description = value; IsChanged = true; }
        }
        public string ImgURL
        {
            get { return Feed.ImgURL; }
            set { Feed.ImgURL = value; IsChanged = true; }
        }

        public string ImgTitle
        {
            get { return Feed.ImgTitle; }
            set { Feed.ImgTitle = value; IsChanged = true; }
        }

        public string ImgLink
        {
            get { return Feed.ImgLink; }
            set { Feed.ImgLink = value; IsChanged = true; }
        }


        public string FtpSite
        {
            get { return Feed.FtpSite; }
            set { Feed.FtpSite = value; IsChanged = true; }
        }

        public string StyleSheet
        {
            get { return Feed.StyleSheet; }
            set { Feed.StyleSheet = value; IsChanged = true; }
        }

        public string StyleType
        {
            get { return Feed.StyleType; }
            set { Feed.StyleType = value; IsChanged = true; }
        }

        public string Subject
        {
            get { return Feed.Subject; }
            set { Feed.Subject = value; IsChanged = true; }
        }
        public DateTime PubDate
        {
            get { return Feed.PubDate; }
            set { Feed.PubDate = value; IsChanged = true; }
        }
        public DateTime LastBuildDate
        {
            get { return Feed.LastBuildDate; }
            set { Feed.LastBuildDate = value; IsChanged = true; }
        }
        private string FileName
        {
            get { return Feed.FileName; }
            set { Feed.FileName = value; IsChanged = true; }
        } 
        #endregion
        #region Constructors
        public RSSObjectDataProvider()
        {
        }
        public RSSObjectDataProvider(string url, IRSSFeedController Controller)
        {
            this.Controller = Controller;
            this.url = url;
            Feed = this.Controller.LoadRss(this.url);
        } 
        #endregion
        #region Load and Save
        public void Load()
        {
            if ((this.controller != null) && (this.url != null))
                Feed = this.controller.LoadRss(this.url);
        }
        public void Save()
        {
            this.controller.SaveFeed(this.Feed);
        } 
        #endregion
        #region Object Data Provider CRUD Methods
        [DataObjectMethod(DataObjectMethodType.Select)]
        public ObservableCollection<RSSItem> GetAllItems()
        {
            return this.Feed.Items;
        }
        [DataObjectMethod(DataObjectMethodType.Select)]
        public RSSItem GetItem(String id)
        {
            for (int i = 0; i < Feed.Items.Count; i++)
                if (Feed.Items[i].Title == id)
                    return this.Feed.Items[i];
            return null;
        }
        [DataObjectMethod(DataObjectMethodType.Update)]
        public bool Update(RSSItem R)
        {

            for (int i = 0; i < this.Feed.Items.Count; i++)
                if (this.Feed.Items[i].Title == R.Title)
                {
                    this.Feed.Items[i] = R;
                    Save();
                    return true;
                }
            return false;

        }
        [DataObjectMethod(DataObjectMethodType.Insert)]
        public bool Add(RSSItem R)
        {
            this.Feed.Items.Insert(0, R);
            Save();
            return true;
        }
        [DataObjectMethod(DataObjectMethodType.Delete)]
        public bool Delete(RSSItem R)
        {
            for (int i = 0; i < this.Feed.Items.Count; i++)
                if (this.Feed.Items[i].Title == R.Title)
                {
                    this.Feed.Items.Remove(this.Feed.Items[i]);
                    Save();
                    break;
                }
            return true;
        } 
        #endregion
       
    }
}